package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.DeleteFriendBean;
import com.zlx.module_base.base_api.res_data.FriendListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CompanionAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<FriendListBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<FriendListBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_companion;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getFriendList();
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<FriendListBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, FriendListBean.InfoDTO.RecordsDTO entity, int position) {
                GlideImgManager.glideLoader(CompanionAc.this, entity.getAvatar(), holder.getView(R.id.iv_follow_head), 0);
                holder.setText(R.id.tv_follow_nickname, entity.getNickName());
                TextView tv_attention = holder.getView(R.id.tv_attention);
                if (StringUt.isEmpty(entity.getType())) {
                    if (entity.getType().equals("1")) {
                        tv_attention.setText("解绑");
                        tv_attention.setTextColor(getResources().getColor(R.color.hot_pink));
                        tv_attention.setBackgroundResource(R.drawable.button_bg_hot_pink_bk5);
                    } else {
                        tv_attention.setText("拍卖中");
                        tv_attention.setTextColor(getResources().getColor(R.color.white));
                        tv_attention.setBackgroundResource(R.drawable.button_bg_hot_pink5);
                    }
                }

                holder.setText(R.id.tv_time, entity.getBirthday());

                ((TextView) holder.getView(R.id.tv_attention)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //解除绑定
                        dialog(position);
                    }
                });
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_companion;
            }
        };

        rv_data.setAdapter(mAdapter);
    }

    private void dialog(int position) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(CompanionAc.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(CompanionAc.this, R.layout.dialog_cancel, null);
        dialog.setContentView(view);

        TextView tv_text = view.findViewById(R.id.tv_text);

        tv_text.setText("是否解除");

        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        TextView tv_determine = view.findViewById(R.id.tv_determine);
        tv_determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                DeleteFriendBean deleteFriendBean = new DeleteFriendBean();
                deleteFriendBean.setId(mList.get(position).getId());
                ApiUtil.getUserApi().addOrDeleteFriend(SPUtil.getToken(CompanionAc.this),
                        deleteFriendBean).observe(CompanionAc.this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<BaseBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    getFriendList();
                                    dialog.dismiss();
                                } else {
                                    Toast.makeText(CompanionAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }));
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

    private void getFriendList() {
        ApiUtil.getUserApi().getFriendList(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<FriendListBean>>() {
            @Override
            public void onSuccess(ApiResponse<FriendListBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    if (null != data.getData().getInfo().get(0).getRecords()) {
                                        if (null != data.getData().getInfo().get(0).getRecords()) {
                                            mList.addAll(data.getData().getInfo().get(0).getRecords());
                                        }
                                    }
                                }
                            }
                        } else {
                            Toast.makeText(CompanionAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    @OnClick({R.id.iv_back, R.id.tv_add_companion})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_companion:
                startActivityForResult(new Intent(this, AddCompanionAc.class), 20);
                break;
        }
    }
}
